package com.tian.contoller;

import com.alibaba.fastjson.JSON;
import com.tian.enums.RabbitMQConstant;
import com.tian.message.StationGunCommunicateLogMessage;
import com.tian.producer.BaseProducer;
import com.tian.message.TestMessage;
import com.tian.util.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-08-23 11:05
 * {@code @version:} 1.0
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Resource
    private BaseProducer baseProducer;

    @RequestMapping("/index")
    public String index() {
        TestMessage testMessage = new TestMessage();
        testMessage.setMsg("hello world");
//        baseProducer.sendSimple(JSON.toJSONString(testMessage), "charge.test.exchange", "charge.test.routingkey");
//        baseProducer.sendWithRetry(JSON.toJSONString(testMessage), 1, "0000000sadas", "charge.test.exchange", "charge.test.routingkey");
        //模仿发送失败的情况
        baseProducer.sendWithRetry(JSON.toJSONString(testMessage), 1, "0000000sadas" + DateUtils.formatDateTime(new Date()), "charge.test.exchange1111", "charge.test.routingkey");
        return "test";
    }

    @RequestMapping("/com/log")
    public String log() {
        StationGunCommunicateLogMessage testMessage = new StationGunCommunicateLogMessage();
        testMessage.setGunNo("2222222222");
        testMessage.setLogType(1);
        testMessage.setParams("12345611111");
        testMessage.setLogTime(LocalDateTime.now());
        testMessage.setReqId("/com/log" + DateUtils.formatDateTime(new Date()));

        //模仿发送失败的情况
        baseProducer.sendWithRetry(JSON.toJSONString(testMessage), 1, testMessage.getReqId(),
                RabbitMQConstant.STATION_GUN_COM_LOG_EXCHANGE, RabbitMQConstant.STATION_GUN_COM_LOG_ROUTING_KEY);
        return "test";
    }
}
